Page({
  data: {
    status: "pending", // pending, approved, rejected
    pendingCount: 0,
    merchants: [],
  },

  onLoad() {
    this.loadMerchants();
  },

  onShow() {
    this.loadMerchants();
  },

  switchStatus(e) {
    const status = e.currentTarget.dataset.status;
    this.setData({ status });
    this.loadMerchants();
  },

  loadMerchants() {
    const { status } = this.data;

    // TODO: 从后端获取商家列表
    // 示例数据
    const mockMerchants = {
      pending: [
        {
          id: "M001",
          name: "示例商家1",
          logo: "/images/default_avatar.png",
          submitTime: "2024-03-20 10:30:00",
          qualifications: [
            {
              type: "business",
              title: "营业执照",
              image: "/images/default_avatar.png",
            },
            {
              type: "health",
              title: "卫生许可证",
              image: "/images/default_avatar.png",
            },
            {
              type: "food",
              title: "食品经营许可证",
              image: "/images/default_avatar.png",
            },
          ],
        },
      ],
      approved: [
        {
          id: "M002",
          name: "示例商家2",
          logo: "/images/default_avatar.png",
          submitTime: "2024-03-19 15:20:00",
          auditTime: "2024-03-19 16:00:00",
          status: "approved",
          qualifications: [
            {
              type: "business",
              title: "营业执照",
              image: "/images/default_avatar.png",
            },
          ],
        },
      ],
      rejected: [
        {
          id: "M003",
          name: "示例商家3",
          logo: "/images/default_avatar.png",
          submitTime: "2024-03-18 09:15:00",
          auditTime: "2024-03-18 10:00:00",
          status: "rejected",
          note: "营业执照已过期",
          qualifications: [
            {
              type: "business",
              title: "营业执照",
              image: "/images/default_avatar.png",
            },
          ],
        },
      ],
    };

    this.setData({
      merchants: mockMerchants[status],
      pendingCount: mockMerchants.pending.length,
    });
  },

  previewImage(e) {
    const url = e.currentTarget.dataset.url;
    wx.previewImage({
      urls: [url],
      current: url,
    });
  },

  handleApprove(e) {
    const merchantId = e.currentTarget.dataset.id;
    wx.showModal({
      title: "确认通过",
      content: "确定通过该商家的入驻申请吗？",
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: "处理中..." });

          // TODO: 调用后端API审核通过商家
          setTimeout(() => {
            wx.hideLoading();
            wx.showToast({
              title: "审核通过",
              icon: "success",
            });
            this.loadMerchants();
          }, 1500);
        }
      },
    });
  },

  handleReject(e) {
    const merchantId = e.currentTarget.dataset.id;
    wx.showModal({
      title: "驳回申请",
      content: "请输入驳回原因",
      editable: true,
      placeholderText: "请输入驳回原因",
      success: (res) => {
        if (res.confirm) {
          if (!res.content) {
            wx.showToast({
              title: "请输入驳回原因",
              icon: "none",
            });
            return;
          }

          wx.showLoading({ title: "处理中..." });

          // TODO: 调用后端API驳回商家申请
          setTimeout(() => {
            wx.hideLoading();
            wx.showToast({
              title: "已驳回",
              icon: "success",
            });
            this.loadMerchants();
          }, 1500);
        }
      },
    });
  },
});
